package com.miniprogram.admin.controller;

import com.miniprogram.common.dto.ResponseVo;
import com.miniprogram.common.enums.RestStatusEnum;
import com.miniprogram.common.utils.RespUtils;
import com.miniprogram.common.utils.Shift;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresUser;
import java.util.List;

import com.miniprogram.admin.service.IAdminRoleService;
import com.miniprogram.bean.po.AdminRole;

/**
 * <p>
 * 后台角色表 前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-06-21
 */
@Api("后台角色表Controller")
@RestController
@Slf4j
@RequestMapping("//admin-role")
public class AdminRoleController extends AbstractController {

    @Autowired
    private IAdminRoleService adminRoleService;

    @ApiOperation(value = "增加接口", notes = "增加传入相关参数",produces="application/json, application/xml", consumes="application/json, application/xml")
    @PostMapping("/insert")
    @RequiresUser
    public ResponseVo<String> insert(AdminRole adminRole) {
        boolean flag = this.adminRoleService.save(adminRole);
        log.info("后台角色表增加{}，增加的id为{}", flag ? "成功" : "失败", adminRole.getId());
        return new ResponseVo<>(RespUtils.respStrByBoolean(flag,"增加"));
    }

    @ApiOperation(value = "根据id全字段更新接口", notes = "必须传入对应的id以及其他所有相关字段",produces="application/json, application/xml", consumes="application/json, application/xml")
    @PostMapping("/updateById")
    @RequiresUser
    public ResponseVo<String> updateById(AdminRole adminRole) {
        boolean flag = this.adminRoleService.updateById(adminRole);
        log.info("后台角色表更新{}，更新的id为{}", flag ? "成功" : "失败", adminRole.getId());
        return new ResponseVo<>(RespUtils.respStrByBoolean(flag,"更新"));
    }

    @ApiOperation(value = "根据id删除接口", notes = "传入id删除对应的数据",produces="application/json, application/xml", consumes="application/json, application/xml")
    @PostMapping("/deleteById")
    @RequiresUser
    public ResponseVo<String> deleteById(Integer id) {
        if (id == null) {
            Shift.fatal(RestStatusEnum.ID_NOTNULL);
        }
        boolean flag = this.adminRoleService.removeById(id);
        log.info("后台角色表删除{}，删除的id为{}", flag ? "成功" : "失败", id);
        return new ResponseVo<>(RespUtils.respStrByBoolean(flag,"删除"));
    }

    @ApiOperation(value = "根据id查询接口", notes = "传入待查询id",produces="application/json, application/xml", consumes="application/json, application/xml")
    @GetMapping("/findById")
    @RequiresUser
    public ResponseVo<AdminRole> findById(Integer id) {
        if (id == null) {
            Shift.fatal(RestStatusEnum.ID_NOTNULL);
        }
        return new ResponseVo<>(this.adminRoleService.getById(id));
    }

    @ApiOperation(value = "查询所有", notes = "查询所有", produces = "application/json, application/xml", consumes = "application/json, application/xml")
    @GetMapping("/findAll")
    public ResponseVo<List<AdminRole>> findAll() {
        return new ResponseVo<>(this.adminRoleService.list());
    }

    @ApiOperation(value = "分页查询", notes = "分页查询", produces = "application/json, application/xml", consumes = "application/json, application/xml")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "current", value = "当前页数", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/findPage")
    public ResponseVo<IPage<AdminRole>> findPage(Page<AdminRole> page) {
        return new ResponseVo<>(this.adminRoleService.page(page));
    }

}

